﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Monitor.Models.JsonModel;
using Monitor.Repository.DataRepository.LinqModel;


namespace Monitor.Controllers.ApiControllers
{
    #region модели

    public class GetModels
    {
       // [System.Runtime.Serialization.DataMember(IsRequired = true)]
        public Int64[] MassiveId { get; set; }

      //  [System.Runtime.Serialization.DataMember(IsRequired = true)]
        public Int64[] MassiveMesuringId { get; set; }

     //   [System.Runtime.Serialization.DataMember(IsRequired = true)]
        public DateTime BTime { get; set; }
    //    [System.Runtime.Serialization.DataMember(IsRequired = true)]
        public DateTime ETime { get; set; }
    }

    public class DataValue
    {
        public Int64 Id_MP { get; set; }
        public Int64 Id_M { get; set; }
        public DateTime Date { get; set; }
        public double Value { get; set; }
    }

    #endregion

    public class ArchiveStateController : MainApiController
    {

        #region GET
        public Collection<DataValue> GetArchiveState(int id, int mp, DateTime begin, DateTime end)
        {
            Collection<DataValue> result = new Collection<DataValue>();
            try
            {
                Random random = new Random(1000);
                for (DateTime current = begin; current < end; current = current.AddMinutes(1))
                {
                    result.Add(new DataValue()
                                   {
                                       Id_M = mp,
                                       Id_MP = id,
                                       Date = current,
                                       Value = random.NextDouble()*100
                                   });
                }
            }
            catch (Exception exception)
            {
                ErrorLogger.Error(this.ControllerContext,exception);
            }
            return result;
        }        
        
        #endregion

        #region POST
        public HttpResponseMessage PostArchiveState(GetModels getModels)
        {
            if (getModels.ETime > DateTime.Now.ToUniversalTime())
                getModels.ETime = DateTime.Now.ToUniversalTime();
            if (ModelState.IsValid && getModels.ETime >= getModels.BTime)
            {
                if (getModels.ETime == getModels.BTime)
                    getModels.ETime = getModels.BTime.AddDays(1);
                try
                {
                    List<CurrentStateRow> result = new List<CurrentStateRow>();
                    Random random=new Random();
                    foreach (Int64 currentPI in getModels.MassiveId)
                    {
                        Monitor.Repository.UserRepository.LinqModel.Car curCur = UserRepository.GetUser(User.Identity.Name).Cars.ToList().FirstOrDefault(n=>n.CarId==currentPI);
                        if (curCur != null)
                        {
                            Car curCar = DataRepository.CarRepository.GetCarById(curCur.Fk_CarId);
                            string carName = curCar.ClassifType.FullName;
                            string carId = curCar.SerialNumber;
                            Int64[] mesuringAccess = curCar.ListOfParam.Select(n => n.ParamId).ToArray();
                            foreach (Int64 currentMI in getModels.MassiveMesuringId)
                                if (mesuringAccess.Contains(currentMI))
                                {
                                    Classif mClassif = DataRepository.ClassificatorRepository.GetById(currentMI);
                                    string mesuringName =mClassif.FullName;
                                    string mesuringTypeName = mClassif.MasterLink.FullName;

                                    for (DateTime curDate = getModels.BTime; curDate < getModels.ETime; curDate = curDate.AddMinutes(1))
                                    {
                                        CurrentStateRow newRow = new CurrentStateRow()
                                                                   {
                                                                       CarId = carId,
                                                                       CarName = carName,
                                                                       DefaultMesure = mesuringName,
                                                                       TypeMesure = mesuringTypeName,
                                                                       DateTimeOfMesure = curDate,
                                                                       Value = random.NextDouble()*1000
                                                                   };
                                        result.Add(newRow);
                                    }


                                }
                        }
                    }

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, result);

                    return response;
                }
                catch (Exception exception)
                {
                    ErrorLogger.Error(this.ControllerContext, exception);
                }
            }
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
        #endregion

    }
}
